package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.customview.turntable.StkTurnTableItem;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.OptionAdapter;
import flc.ast.databinding.ActivitySetOptionBinding;
import flc.ast.dialog.AddOptionDialog;
import flc.ast.utils.AppSpUtils;

public class SetOptionActivity extends BaseAc<ActivitySetOptionBinding> {

    private OptionAdapter optionAdapter;
    private List<String> listData = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.activity_set_option;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetOptionBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.rvOptionList.setLayoutManager(new LinearLayoutManager(mContext));
        optionAdapter = new OptionAdapter();
        mDataBinding.rvOptionList.setAdapter(optionAdapter);
        optionAdapter.addChildClickViewIds(R.id.ivOptionItemDelete);
        optionAdapter.setOnItemClickListener(this);
        optionAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                AppSpUtils.saveTurntableList(listData);
                ToastUtils.showShort(getString(R.string.save_success));
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (TextUtils.isEmpty(optionAdapter.getItem(position))) {
            showAddOptionDialog();
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivOptionItemDelete://删除
                listData.remove(position);
                List<String> listShow = new ArrayList<>() ;
                listShow.addAll(listData);
                if (listData.size() < 8) listShow.add("");
                optionAdapter.setList(listShow);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listData.clear();
        List<String> listSp = AppSpUtils.getTurntableList();
        listData.addAll(listSp);
        List<String> listShow = new ArrayList<>();
        listShow.addAll(listData);
        if (listData != null && listData.size() < 8) listShow.add("");
        optionAdapter.setList(listShow);
    }

    private void showAddOptionDialog() {
        AddOptionDialog dialog = new AddOptionDialog(mContext);

        dialog.setListener(new AddOptionDialog.Ilistener() {
            @Override
            public void clickConfirm(String text) {
                listData.add(text);
                List<String> listShow = new ArrayList<>();
                listShow.addAll(listData);
                if (listData.size() < 8) listShow.add("");
                optionAdapter.setList(listShow);
            }
        });

        dialog.show();
    }

}